local famBags = LibStub('AceAddon-3.0'):GetAddon('famBags')
famBags.LDBBags = {}
-- :::::::::::: LOCALS ::::::::::::: -------------------------------------------------------------------------

local color1 = {255,0,0}
local color2 = {153,255,0}
local color = {1,1,1}

local function get_color_perc(c1, c2, coef)
   coef = sqrt(coef)
   if c1 > c2 then
      return floor(c1 - ((c1 - c2) * coef))
   elseif c1 < c2 then
      return floor(c1 + ((c2 - c1) * coef))
   end
   return c1
end

-- :::::::::::: famBags.LDBBags ::::::::::::: -------------------------------------------------------------------------
function famBags.LDBBags:Create()
   if not self.LDB then
      self.LDB = LibStub("LibDataBroker-1.1"):NewDataObject("famBags Bags", {
         type  = 'data source', 
         icon	= 'Interface\\Icons\\INV_Misc_Bag_07',
         label	= famBags.L['famBags Bags'],
	    	OnClick = function(frame, button)
	    	   if button == 'LeftButton' then
	        	   OpenAllBags()
	        	elseif button == 'RightButton' then
	        	   famBags:LoadOptions()
	        	end
	    	end,
      })
   end
end

function famBags.LDBBags:Update()
   local slots, free = 0, 0
   for _, i in ipairs(famBags.BagIndexes.bags) do
      if i ~= KEYRING_CONTAINER then
         slots = slots + (GetContainerNumSlots(i) or 0)
         free = free + (GetContainerNumFreeSlots(i) or 0)
      end
   end
   local coef = free / slots
   self.LDB.text = string.format(
      '|cff%02x%02x%02x%s|r', 
      get_color_perc(color1[1], color2[1], coef), 
      get_color_perc(color1[2], color2[2], coef), 
      get_color_perc(color1[3], color2[3], coef), 
      free..'/'..slots)
end

